{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from tqdm import tqdm\n",
    "import shutil\n",
    "import pickle\n",
    "from config.root_path import WEIGHT_ROOT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_root = 'result_lora'\n",
    "template_path = './result/template'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def copy_result_folder(result_folder):\n",
    "    weight_path = os.path.join(WEIGHT_ROOT, result_folder)\n",
    "    result_path = f'{result_root}/{result_folder}'\n",
    "    if not os.path.exists(result_path):\n",
    "        os.makedirs(result_path)\n",
    "    if os.path.exists(weight_path):\n",
    "        shutil.rmtree(weight_path)\n",
    "    os.makedirs(weight_path)\n",
    "\n",
    "    for item in os.listdir(template_path):\n",
    "        s = os.path.join(template_path, item)\n",
    "        d = os.path.join(weight_path, item)\n",
    "        if os.path.isdir(s):\n",
    "            shutil.copytree(s, d, False, None)\n",
    "        else:\n",
    "            shutil.copy2(s, d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_folder = 'maxvit_lora'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'maxvit'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = result_folder.split('_')[0]\n",
    "net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "copy_result_folder(result_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "for folder in os.listdir('result_large_model'):\n",
    "    copy_result_folder(folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CUDA_VISIBLE_DEVICES=3,4,5,6,7,8,9\n"
     ]
    }
   ],
   "source": [
    "print('CUDA_VISIBLE_DEVICES=3,4,5,6,7,8,9')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'nohup accelerate launch --num_processes=4  train_wandb.py  -n maxvit -r maxvit_lora  > ./nohup.out &'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distributed_conmand = f'nohup accelerate launch --num_processes=4  train_wandb.py  -n {net} -r {result_folder}  > ./nohup.out &'\n",
    "distributed_conmand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accelerate launch --num_processes=4  predict_faster.py  -n efficientnet -r efficientnet_v2_m\n",
      "wait\n",
      "accelerate launch --num_processes=4  predict_faster.py  -n resnet101 -r resnet101_b64\n",
      "wait\n",
      "accelerate launch --num_processes=4  predict_faster.py  -n swin -r swin_v2_b\n",
      "wait\n",
      "accelerate launch --num_processes=4  predict_faster.py  -n wide_resnet -r wide_resnet50\n",
      "wait\n",
      "accelerate launch --num_processes=4  predict_faster.py  -n vit -r vit\n",
      "wait\n",
      "accelerate launch --num_processes=4  predict_faster.py  -n convnext -r convnext\n",
      "wait\n",
      "accelerate launch --num_processes=4  predict_faster.py  -n regnet -r regnet\n",
      "wait\n",
      "accelerate launch --num_processes=4  predict_faster.py  -n maxvit -r maxvit\n",
      "wait\n"
     ]
    }
   ],
   "source": [
    "for folder in os.listdir('result_large_model'):\n",
    "    if folder == 'wide_resnet50':\n",
    "        net = 'wide_resnet'\n",
    "    else:\n",
    "        net = folder.split('_')[0]\n",
    "    # distributed_conmand = f'nohup accelerate launch --num_processes=4  train_wandb.py  -n {net} -r {folder}  > ./nohup.out &'\n",
    "    predict_cm = f'accelerate launch --num_processes=4  predict_faster.py  -n {net} -r {folder}'\n",
    "    print(predict_cm)\n",
    "    print('wait')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = result_folder.split('_')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'nohup python train_faster.py -n ScConv18 -r ScConv18 >  &'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conmand = f'nohup python train_faster.py -n {net} -r {result_folder} >  &'\n",
    "conmand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_copy('copy_ssl','ssl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!sh script/copy_north.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root= ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sum  =0 \n",
    "result = []\n",
    "for folder in os.listdir(root):\n",
    "    for file in tqdm(os.listdir(os.path.join(root,folder))):\n",
    "        with open(os.path.join(root,folder,file),'rb') as f:\n",
    "            data = pickle.load(f)\n",
    "            for item in data:\n",
    "                if item[1] != 'artifact':\n",
    "                    result.append((folder,file,item))\n",
    "                    sum+=1\n",
    "sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./predict_result/total.pkl','wb') as f:\n",
    "    pickle.dump(result,f)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolov5",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
